package cn.stimd.spring.context.support;

import cn.stimd.spring.beans.BeansException;
import cn.stimd.spring.beans.factory.config.ConfigurableBeanFactory;
import cn.stimd.spring.beans.factory.support.DefaultListableBeanFactory;

import java.io.IOException;

/**
 * 主要应用于通过配置文件创建Context的情况，一旦配置文件变更，可以动态地刷新整个容器。
 * 1. 持有一个非特定的BeanFactory，每次刷新都会创建新的BeanFactory
 * 2. 刷新操作会加载配置文件的信息
 * <p>
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext{
    private DefaultListableBeanFactory beanFactory;

    @Override
    protected void refreshBeanFactory() throws BeansException {
        try{
            this.beanFactory = new DefaultListableBeanFactory();
            loadBeanDefinitions(beanFactory);
        }catch (IOException e){
            throw new BeansException("解析BeanDefinition时发生IO错误");
        }
    }


    //模版方法，由子类实现具体的加载逻辑
    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
            throws BeansException, IOException;


    @Override
    public ConfigurableBeanFactory getBeanFactory() {
        return beanFactory;
    }
}
